function saveQuestionToQuestion(f, e, d) {
    if (f.questions != null && f.questions.length > 0) {
        var a = -1;
        var g = "";
        if (d != undefined && d != "") {
            g = d
        } else {
            g = e.id
        }
        for (var c = 0; c < f.questions.length; c++) {
            if (g == f.questions[c].id) {
                a = c
            }
        }
        if (a > -1) {
            if (d != undefined && d != "") {
                f.questions.splice(a + 1, 0, e)
            } else {
                f.questions.splice(a, 1, e)
            }
        } else {
            f.questions.push(e)
        }
    } else {
        var b = new Array();
        b.push(e);
        f.questions = b
    }
    questionMap[f.id] = f
}
function removeQuestionToQuestion(c, d) {
    var a = -1;
    for (var b = 0; b < c.questions.length; b++) {
        if (d == c.questions[b].id) {
            a = b
        }
    }
    c.questions.splice(a, 1);
    questionMap[c.id] = c
}
function saveAnswerToQuestion(f, b, g) {
    if (f.answers != null && f.answers.length > 0) {
        var h = "";
        if (g != undefined && g != "") {
            h = g
        } else {
            h = b.id
        }
        var c = -1;
        for (var e = 0; e < f.answers.length; e++) {
            if (h == f.answers[e].id) {
                c = e
            }
        }
        if (c > -1) {
            if (g != undefined && g != "") {
                f.answers.splice(c + 1, 0, b)
            } else {
                f.answers.splice(c, 1, b)
            }
        } else {
            f.answers.push(b)
        }
    } else {
        var d = new Array();
        d.push(b);
        f.answers = d
    }
    questionMap[f.id] = f
}
function removeAnswerToQuestion(d, c) {
    var a = -1;
    for (var b = 0; b < d.answers.length; b++) {
        if (c == d.answers[b].id) {
            a = b
        }
    }
    d.answers.splice(a, 1);
    questionMap[d.id] = d
}
function getMaxPoint(c) {
    var a = 0;
    for (var b = 0; b < c.answers.length; b++) {
        if (a < c.answers[b].point) {
            a = c.answers[b].point
        }
    }
    return a
}
function showTemplateQuestion() {
    var b = "";
    b += '<div  class="save-info" style="width: 420px;">';
    b += '<h2 id="templateshow">请从以下题库中选择您需要的问题选项</h2>';
    b += '<p class="gray">从左侧题库中选择问题类型，在右侧中修改选项后添加</p>';
    b += "</div>";
    b += "";
    b += '<table cellspacing="0">';
    b += "<tbody><tr>";
    b += '<td id="choiceMenu"><ul id="templatequestionlist"></ul></td>';
    b += '<td class="flow"><div>→</div></td>';
    b += '<td id="choiceText"><textarea id="templatequestionanswerlist" class="textarea" rows="10" cols="50"></textarea></td>';
    b += "</tr></tbody>";
    b += "</table>";
    b += "";
    b += '<div class="bulkButton">';
    b += '<span id="showtempleteerror" style="display:none;" class="missing mr10"></span>';
    b += ' <a href="javascript:void(0);" onclick="addTemplateQuestion();return false;" class="button"> <img src="' + ctx + '/app/design/resource/images/text_list_bullets.png" alt=""> 添加至我的选项 </a>';
    b += ' <a href="javascript:void(0);" onclick="hideTemplateQuestion();return false;" class="lbOff button negative"> <img src="' + ctx + '/app/design/resource/images/cross.png" alt=""> 取消 </a> ';
    b += "</div>";
    b += "";
    b += "";
    var a = {
        id: "TemplateQuestionDIV",
        width: "420px",
        fixed: true,
        window: "top",
        padding: " 15px",
        title: "",
        lock: true,
        content: b,
        esc: false,
        dblClickUnlock: "disabled",
        initialize: function() {
            $("#templatequestionlist").html(getTemplateQuestionListHtml())
        },
        beforeunload: function() {}
    };
    art.dialog(a)
}
function hideTemplateQuestion() {
    closeArtDialog("TemplateQuestionDIV")
}
function addTemplateQuestion() {
    var g = $("#templatequestionanswerlist").val().replace(/(\r)*\n/g, "@#@");
    var f = g.split("@#@");
    var b = questionMap[targetid];
    if ((b.type == 1 || b.type == 2) && (b.answers.length + f.length) > answerCount && answerCount != 0) {
        showErrorMsg("showtempleteerror", "showtempleteerror", formatMessage("edit_009", answerCount));
        return
    }
    if ((b.type == 41 || b.type == 42) && (b.answers.length + f.length) > childAnswerCount && childAnswerCount != 0) {
        showErrorMsg("showtempleteerror", "showtempleteerror", formatMessage("edit_009", childAnswerCount));
        return
    }
    recordQuestionEdit(targetid);
    var k = (new Date()).getTime();
    var e = "";
    var a = "";
    if (b.allowother == 1) {
        a = b.answers[b.answers.length - 2].id
    } else {
        a = b.answers[b.answers.length - 1].id
    }
    var j = getMaxPoint(b);
    for (var d = 0; d < f.length; d++) {
        var h = f[d].replace(/\s/g, "").replace(/"/g, "“").replace(/'/g, "‘");
        var c = new Answer({
            name: replaceNormalHTML(h),
            id: k + d
        });
        c.point = j + (d + 1);
        saveAnswerToQuestion(b, c, a);
        a = c.id
    }
    questionMap[b.id] = b;
    $("#qleftanswers").html(getLeftQuestionAnswersHtml(b, checkPointIsHidden(b.id)));
    $("#q_answers_" + b.id).html(getQuestionAnswerHtml(b));
    if (b.type == 1 && $("#qleftanswers").find(".drag-on").size() >= 10) {
        checkAnswersTips(b.id, 0)
    }
    refreshQuestionHeight(b.id);
    $("#templatequestionanswerlist").val("");
    hideTemplateQuestion();
    $("#qleftanswers").children(".drag-on").each(function() {
        var i = new RegExp(/^(选项)\d*$/);
        if (i.test($(this).children("input").first().val()) == true) {
            $(this).children("a").each(function() {
                if ($(this).attr("name") == "btn_del") {
                    $(this).trigger("click")
                }
            })
        }
    });
    showLeftQuestion(targetid)
};